<!DOCTYPE html>
<html>
	<head>
		 <meta http-equiv="refresh" content="86400">
		 	<title>BIOS更新监视后台程序</title>
	</head>
	<body>
<?php
	include 'inc/conn.php';
	include 'sendmail.php';

	extract($_POST);
	extract($_GET);
	unset($_POST,$_GET);

	$conn = ftp_connect("ftp.tw.tongfangpc.com") or die("Could not connect");
		
	ftp_login($conn, "Sw120_ftpguest",	"ycNwKT12");
	//ftp_pasv($conn, false);
	
	$db = new mysql();
	$db->query('select * from bios_monitor where `status`=1');
	$num = $db->db_num_rows();

	for($ii=0; $ii<$num; $ii++){
		$row = $db->fetch_assoc();
		
		$monDir="/SW BIOS+EC Image Release/${row['BIOS_DIR']}/BIOS";
		$monDate="${row['BIOS_DATE']}";

		ftp_chdir($conn, $monDir);
		echo ftp_pwd($conn) . "<br />\n";
		
		$arrDir=ftp_rawlist($conn, ".");
		$arrDirLength=count($arrDir);

		//print_r(ftp_nlist($conn,"."));
		$sList="Dear all,<br />【${row['BIOS_DIR']}】客制化BIOS，因【${row['BIOS_Reason']}】的原因 且 依据日期【${row['BIOS_DATE']}】切入后，更新清单如下：<br /><br />$monDir<br />";
		$i=2;
		$passNum=0;
		$failNum=0;
		for ($i=2; $i<$arrDirLength; $i++)
		{
			$isPass='false';
			$aDir=strrchr($arrDir[$i], ' ');
			if(ftp_chdir($conn,$monDir."/".$aDir)){
				echo "<br><stong><font color='blue'>".ftp_pwd($conn)."</font></stong>";
				
				$arrFiles=ftp_rawlist($conn, ".");
				
				foreach($arrFiles as $x=>$fName) {
					$aFile=trim(strrchr($fName, ' '));
					if($aFile!='..' && $aFile!='.'){
						//echo "<br>'$aFile'\n";
						$numMatched = preg_match('/\d{8}/', $aFile, $arrMatches);
						if($numMatched>0)
							//var_dump($numMatched, $arrMatches);
							if(strcmp($arrMatches[0], $monDate)>=0) $isPass='true';
					}
				}
				
				if($isPass=='true'){
					echo '====='.$aDir." is <font color='green'>已更新</font>";
					$sList.="<b>$aDir: </b><font color='green'>已更新</font><br />";
					$passNum++;
				}else{
					echo '====='.$aDir." is <font color='red'>未更新</font>";
					$sList.="<b>$aDir: </b><font color='red'>未更新</font><br />";
					$failNum++;
				}
			}
		}
		echo "<br />";

		$numCust=$arrDirLength-3; // for std and . and .. folder
		$failNum=$failNum-1;  // for std or standard folder

		if($failNum<=0){  // update after check all are pass
			$db->query("update bios_monitor set `status`=0, `Close_Reason`='由系统分析行为关闭' where ID=${row['ID']}");
		}

		$result=$sList."<br /><b>合计客制化：</b>$numCust <b>个</b>，<b>已导数量：</b><font color='blue'>$passNum</font> ，<b>未导数量：</b><font color='red'>$failNum</font><br />";

		if(empty($mail)) echo $result;

		if(isset($mail) && $mail==1 && $failNum>0){  // only found false and sent mail
			SendMail("【${row['BIOS_DIR']}】客制化BIOS分析结果", $result, "PT");
		}

		echo "<br />===========================================================================<br />\n";
	}
	$db->close();
	ftp_close($conn);
?>
	</body>
</html>